<?php

namespace app\controller;

use app\services\Jwt;
use app\services\WechatPayService;
use app\services\WechatService;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\PriceLog;
use plugin\admin\app\model\User;
use plugin\admin\app\model\Withdraw;
use support\Request;

class WechatController
{

    public function verifyToken(Request $request)
    {
        return WechatService::index($request);
    }

    public function codeUrl(Request $request)
    {
        $url = $request->get('url');
        $url = WechatService::login($url);

        return json(['code'=>0,'msg'=>'success','data'=>['url'=>$url]]);
    }


    public function login(Request $request)
    {
        $params = $request->all();
        $info = WechatService::userFromCode($params['code']);
        $id = $info->getId();
        if (!empty($id)){
            $user = User::firstOrCreate([
                'openid'=>$id,
            ],[
                'openid'=>$id,
                'username' => $id,
                'nickname' => $info->getNickname()??$id,
            ]);
        }
        $token = Jwt::createjwt($user->id);
        return json([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'token' => $token,
                'openid' => $id,
                'nickname' => $user->nickname,
                'bind_mobile' => empty($user->mobile)?0:1,
            ]
        ]);
    }

    public function userInfo(Request $request)
    {
        $token = $request->header('token','');
        $res = Jwt::verifyjwt($token);
        if (!$res['status']){
            return json(['code'=>999,'msg'=>$res['msg']]);
        }
        $user = User::where('id',$res['id'])->first(['id','nickname','mobile','openid','money']);
        $user['price_log'] = PriceLog::sum('price');
        $user['reward_price'] = Order::where('is_reward',1)->sum('reward_total_price');
        return json(['code'=>0,'msg'=>'success','data'=>$user]);

    }

    public function withdraw(Request $request)
    {
        $token = $request->header('token','');
        $res = Jwt::verifyjwt($token);
        if (!$res['status']){
            return json(['code'=>999,'msg'=>$res['msg']]);
        }
        $params = $request->post();
        $user = User::where('id',$res['id'])->first();
        if ($user->money < $params['price']){
            return json(['code'=>1,'msg'=>'可提现余额不足']);
        }
        $user->money = $user->money - $params['price'];
        $user->save();
        Withdraw::create([
            'sn' =>Withdraw::getSn(),
            'user_id' => $user->id,
            'price' => $params['price'],
            'name' => $params['name'],
            'card' => $params['card'],
            'bank_id' => $params['bank_id']
        ]);
        return   json(['code'=>0,'msg'=>'申请提现成功']);

    }

    public function withdrawList(Request $request)
    {
        $token = $request->header('token','');
        $res = Jwt::verifyjwt($token);
        if (!$res['status']){
            return json(['code'=>999,'msg'=>$res['msg']]);
        }
        $status = $request->get('status','');
        $where = [];
        if ($status==0){
            $where[] = ['status','=',0];
        }
        if ($status==1){
            $where[] = ['status','=','1'];
        }
        $list = Withdraw::where('user_id',$res['id'])->where($where)->orderBy('id','desc')->select()->get()->toArray();
        return json(['code'=>0,'msg'=>'success','data'=>$list]);

    }

//    public function addOrder(Request $request)
//    {
//        $token = $request->header('token','');
//        $res = Jwt::verifyjwt($token);
//        if (!$res['status']){
//            return json(['code'=>999,'msg'=>$res['msg']]);
//        }
//        $params = $request->post();
//        if ($params['unique_id']){
//            return json(['code'=>1,'msg'=>'参数错误']);
//        }
//        $openid = User::where('id',$res['id'])->value('openid');
//        $order = WechatPayService::orderUnify($openid,$params['unique_id']);
//    }

    public function notify(Request $request)
    {
        return WechatPayService::notify($request);

    }

    public function getConfig(Request $request)
    {
        $api = [
            'chooseWXPay',
            'getBrandWCPayRequest'
        ];
        $_SERVER['HTTPS'] = 'on'; // 如果你是https的话加上这句
        $_SERVER['HTTP_HOST'] = $request->host();
        $_SERVER['REQUEST_URI'] = $request->uri();
        $result = WechatService::buildConfig($api);
        return json(['code'=>0,'msg'=>'success','data'=>$result]);
    }

    public function setMenu()
    {
        $menu = [
            [
                "type" => "view",
                "name" => "汽车公社",
                "url"  => "https://car.ydk1.cn/h5/index.html#/?id=67b1ce3c8e6df"
            ],
            [
                'type' => 'view',
                'name' => '订车会员',
                'url'  => 'https://car.ydk1.cn/index/carOrder?goods_id=1'
            ]
        ];
        $a = WechatService::createMenu($menu);
        dd($a);
    }

}